package com;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;

public class MainApp {
    private static Trader raoul = new Trader("Raoul", "Cambridge");
    private static Trader mario = new Trader("Mario", "Milan");
    private static Trader alan = new Trader("Alan", "Cambridge");
    private static Trader brian = new Trader("Brian", "Cambridge");

    /**
     * 测试数据
     */
    private static List<Transaction> transactionList = Arrays.asList(
            new Transaction(brian, 2011, 300),
            new Transaction(raoul, 2012, 1000),
            new Transaction(raoul, 2011, 400),
            new Transaction(mario, 2012, 710),
            new Transaction(mario, 2012, 700),
            new Transaction(alan, 2012, 950)
    );

    public static void main(String[] args) {
        System.out.println("请选择功能：");
        Scanner input = new Scanner(System.in);
        int choose = input.nextInt();
        switch (choose) {
            case 1:
                test1();
                break;
            case 2:
                test2();
                break;
            case 3:
                test3();
                break;
            case 4:
                test4();
                break;
            case 5:
                test5();
                break;
            case 6:
                test6();
                break;
            case 7:
                test7();
                break;
            case 8:
                test8();
                break;
        }
    }
    /**
     * 找出`2011`年发生的所有交易，并按交易额排序（从高到低）
     */
    public static void test1(){
        transactionList.stream()
                .filter(transaction -> transaction.getYear()==2011)
                .sorted((t1,t2)->t2.getValue()-t1.getValue())
                .forEach(System.out::println);
    }
    /**
     * 交易员都在哪些不同的城市工作过？
     */
    public static void test2(){
        transactionList.stream()
                .map(transaction -> transaction.getTrader())
                .map(trader -> trader.getCity())
                .distinct()
                .forEach(System.out::println);
    }
    /**
     * 查找所有来自`Cambridge`的交易员，并按姓名排序。
     */
    public static void test3(){
        transactionList.stream()
                .map(transaction -> transaction.getTrader())
                .filter(trader -> trader.getCity().equals("Cambridge"))
                .distinct()
                .sorted((t1,t2)->t1.getName().compareTo(t2.getName()))
                .forEach(System.out::println);
    }

    /**
     * 返回所有交易员的姓名字符串，按字母顺序排序。
     */
    public static void test4(){
        transactionList.stream()
                .map(transaction -> transaction.getTrader())
                .map(trader -> trader.getName())
                .distinct()
                .sorted((t1,t2)->t1.compareTo(t2))
                .forEach(System.out::println);
    }
    /**
     * 有没有交易员在Milan工作
     */
    public static void test5(){
        transactionList.stream()
                .map(transaction -> transaction.getTrader())
                .filter(trader -> trader.getCity().equals("Milan"))
                .distinct()
                .forEach(System.out::println);
    }

    /**
     * 打印生活在Cambridge的交易员的所有交易额。
     */
    public static void test6(){
        transactionList.stream()
                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
                .map(transaction -> transaction.getValue())
                .forEach(System.out::println);
    }

    /**
     * 所有的交易额中，最高的交易额是多少？
     */
    public static void test7(){
        Optional<Integer> max = transactionList.stream()
                .map(transaction -> transaction.getValue())
                .max((t1, t2) -> t1.compareTo(t2));
        System.out.println(max);
    }

    /**
     * 所有的交易额中，最低的交易额是多少？
     */
    public static void test8(){
        Optional<Integer> max = transactionList.stream()
                .map(transaction -> transaction.getValue())
                .min((t1, t2) -> t1.compareTo(t2));
        System.out.println(max);
    }
}
